﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;

namespace OrderService
{
    
    public class OrderService : IOrderService
    {
        
        public int generateQuote(string name, string email, String[] parts, int cost, String size, int repair_time)
        {
            SqlConnection connection = new SqlConnection(
                "Data Source=fastapps04.qut.edu.au;Initial Catalog=n7251998;User ID=n7251998;Password=inb374");
            connection.Open();

            SqlCommand command = new SqlCommand("SELECT C_ID, Customer, Email, OrderStatus, Parts, TotalCost, Start_date, End_date, Vehicle_size, Repair_time FROM OrderTable", connection);
            //SqlDataReader reader;
            string quote = "quote";
            string start = "Not defined";
            string end = "Not defined";
            string temp_parts = string.Join(",", parts);
            string query = @"INSERT INTO OrderTable (Customer, Email, OrderStatus, Parts, TotalCost, Start_date, End_date, Vehicle_size, Repair_time) OUTPUT INSERTED.C_ID VALUES  ('" + name + "','" + email + "','" + quote + "','" + temp_parts + "','" + cost + "','" + start + "','" + end + "','" + size + "','" + repair_time + "')";
    
            command.CommandText = query;
            Int32 newID = (Int32)command.ExecuteScalar();

            connection.Close();

            return newID;
        }

        public Order[] getOrders()
        {
            Order[] parts;
            List<Order> order_list = new List<Order>();

            SqlConnection connection = new SqlConnection(
                "Data Source=fastapps04.qut.edu.au;Initial Catalog=n7251998;User ID=n7251998;Password=inb374");
            connection.Open();

            SqlCommand command = new SqlCommand("SELECT C_ID, Customer, Email, OrderStatus, Parts, TotalCost, Start_date, End_date, Vehicle_size, Repair_time FROM OrderTable", connection);

            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                String[] temp_parts;
                temp_parts = reader.GetString(4).Split(',');

                Order temp = new Order(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), temp_parts, reader.GetInt32(5), reader.GetString(6), reader.GetString(7),reader.GetString(8), reader.GetInt32(9));
                order_list.Add(temp);
            }

            parts = order_list.ToArray();
            reader.Close();
            connection.Close();

            return parts;
        }

        


        public void setStatus(int id, String status)
        {
            SqlConnection connection = new SqlConnection(
                "Data Source=fastapps04.qut.edu.au;Initial Catalog=n7251998;User ID=n7251998;Password=inb374");
            connection.Open();

            SqlCommand command = new SqlCommand("SELECT C_ID, Customer, Email, OrderStatus, Parts, TotalCost, Start_date, End_date, Vehicle_size FROM OrderTable", connection);

            string updateQuery = @"UPDATE OrderTable SET OrderStatus = '" + status + "' WHERE  (C_ID = '" + id + "')";
            command.CommandText = updateQuery;
            command.ExecuteNonQuery();

            connection.Close();
        }


        public Order getOrderByID(int id)
        {

            Order temp = null; // TODO FIX, BAD BAD BAD...

            SqlConnection connection = new SqlConnection(
                "Data Source=fastapps04.qut.edu.au;Initial Catalog=n7251998;User ID=n7251998;Password=inb374");
            connection.Open();

            SqlCommand command = new SqlCommand("SELECT C_ID, Customer, Email, OrderStatus, Parts, TotalCost, Start_date, End_date, Vehicle_size, Repair_time FROM OrderTable WHERE C_ID = " + id, connection);

            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                String[] temp_parts;
                temp_parts = reader.GetString(4).Split(',');

                temp = new Order(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), temp_parts, reader.GetInt32(5), reader.GetString(6), reader.GetString(7), reader.GetString(8), reader.GetInt32(9));
                
            }

            
            reader.Close();
            connection.Close();

            return temp;
        }



        public void setStartEnd(int id, string startDate, string endDate)
        {
            SqlConnection connection = new SqlConnection(
                "Data Source=fastapps04.qut.edu.au;Initial Catalog=n7251998;User ID=n7251998;Password=inb374");
            connection.Open();

            SqlCommand command = new SqlCommand("SELECT C_ID, Customer, Email, OrderStatus, Parts, TotalCost, Start_date, End_date, Vehicle_size FROM OrderTable", connection);

            string updateQuery = @"UPDATE OrderTable SET Start_date = '" + startDate + "', " + "End_date = '" + endDate + "' WHERE  (C_ID = '" + id + "')";

            command.CommandText = updateQuery;
            command.ExecuteNonQuery();

            connection.Close();
        }
    }
}
